<?php
/**
 * CakeCMS - 角色管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class RoleController extends AppController {
	public $components = array (
			'CheckUserPermission' 
	);
	public $helpers = array (
			'Page' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', '管理员设置' );
	}
	public function index() {
		$this->set ( 'title_for_layout', '角色管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'role.js' 
		) );
		
		$this->loadModel ( "AdminRole" );
		$conditions = array ();
		$Rolelist = $this->AdminRole->GetAdminRoleByAll ( $conditions );
		
		$this->set ( 'Rolelist', $Rolelist );
	}
	
	/**
	 * 权限设置左侧导航
	 */
	public function priv_setting() {
		$roleid = $this->params ['roleid'];
		
		$this->loadModel ( "AdminRole" );
		$conditions = array ();
		$role_list = $this->AdminRole->GetAdminRoleByAll ( $conditions );
		
		$this->set ( 'roleid', $roleid );
		$this->set ( 'role_list', $role_list );
	}
	
	/**
	 * 权限设置
	 */
	public function rolepriv() {
		if ($this->request->isPost ()) {
			$roleid = $this->data ['roleid'];
			$menuid = $this->data ['menuid'];
			
			$this->loadModel ( "AdminRolePriv" );
			$AdminRolePrivInfo = $this->AdminRolePriv->GetAdminRolePrivByID ( $roleid );
			if ($AdminRolePrivInfo) {
				$data ['AdminRolePriv'] ['roleid'] = $roleid;
				$data ['AdminRolePriv'] ['data'] = serialize ( $menuid );
				$return = $this->AdminRolePriv->edit ( $data );
			} else {
				$data ['roleid'] = $roleid;
				$data ['data'] = serialize ( $menuid );
				$return = $this->AdminRolePriv->add ( $data );
			}
			if ($return) {
				$this->flash ( '设置权限成功!', "/Role/rolepriv/{$roleid}/", 1250 );
				return false;
			} else {
				$this->flash ( '设置权限失败!', "/Role/rolepriv/{$roleid}/", 1250 );
				return false;
			}
		} else {
			$roleid = $this->params ['roleid'];
			// 加载js文件
			$this->set ( 'scriptList', array (
					'jquery.treetable.js' 
			) );
			
			$this->set ( 'roleid', $roleid );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$tree->icon = array (
					'│ ',
					'├─ ',
					'└─ ' 
			);
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			
			$this->loadModel ( "Menu" );
			
			$conditions = array ();
			$result = $this->Menu->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'listorder ASC',
							'id ASC' 
					) 
			) );
			
			$this->loadModel ( "AdminRolePriv" );
			$AdminRolePrivInfo = $this->AdminRolePriv->GetAdminRolePrivByID ( $roleid );
			
			if ($AdminRolePrivInfo) {
				$data = unserialize ( $AdminRolePrivInfo ['AdminRolePriv'] ['data'] );
			} else {
				$data = array ();
			}
			
			$this->loadModel ( "AdminRole" );
			
			$array = array ();
			
			foreach ( $result as $n => $t ) {
				$r ['id'] = $t ['Menu'] ['id'];
				$r ['name'] = $t ['Menu'] ['name'];
				$r ['parentid'] = $t ['Menu'] ['parentid'];
				$r ['controller'] = $t ['Menu'] ['controller'];
				$r ['data'] = $t ['Menu'] ['data'];
				$r ['listorder'] = $t ['Menu'] ['listorder'];
				$r ['display'] = $t ['Menu'] ['display'];
				
				$r ['checked'] = (in_array ( $t ['Menu'] ['id'], $data )) ? ' checked' : '';
				$r ['level'] = $this->Menu->get_level ( $t ['Menu'] ['id'], $result );
				$r ['parentid_node'] = ($t ['Menu'] ['parentid']) ? ' class="child-of-node-' . $t ['Menu'] ['parentid'] . '"' : '';
				$array [] = $r;
			}
			$str = "<tr id='node-\$id' \$parentid_node>
			<td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuid[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
			</tr>";
			
			$tree->init ( $array );
			$categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'categorys', $categorys );
		}
	}
	
	/**
	 * 栏目设置
	 */
	public function setting_cat_priv() {
		$roleid = $this->params ['roleid'];
		
		$this->loadModel ( "AdminRole" );
		$conditions = array ();
		$role_list = $this->AdminRole->GetAdminRoleByAll ( $conditions );
		
		$this->set ( 'roleid', $roleid );
		$this->set ( 'role_list', $role_list );
	}
	
	/**
	 * 栏目权限设置
	 */
	public function columnpriv() {
		$roleid = $this->params ['roleid'];
		$this->set ( 'roleid', $roleid );
	}
	
	/**
	 * 成员管理
	 */
	public function member() {
		$roleid = $this->params ['roleid'];
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'administrator.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Admin" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array (
				'roleid' => $roleid 
		);
		
		$list = $this->Admin->GetAdminByList ( $conditions, $limit );
		
		if (! empty ( $list ['result'] )) {
			$this->loadModel ( "AdminRole" );
			foreach ( $list ['result'] as &$val ) {
				$Role_info = $this->AdminRole->GetAdminRoleByID ( $val ['Admin'] ['roleid'] );
				$val ['Admin'] ['rolename'] = $Role_info ['AdminRole'] ['rolename'];
				$val ['Admin'] ['lastlogintime'] = date ( "Y-m-d H:i:s", $val ['Admin'] ['lastlogintime'] );
			}
		}
		
		$this->set ( 'list', $list ['result'] );
		$this->set ( 'roleid', $roleid );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 添加角色
	 */
	public function add() {
		if ($this->request->isPost ()) {
			
			$data ['AdminRole'] ['rolename'] = $this->data ['rolename'];
			if (empty ( $data ['AdminRole'] ['rolename'] )) {
				$this->flash ( '角色名称不能为空!', '/Role/add/', 1250 );
				return false;
			}
			$data ['AdminRole'] ['description'] = $this->data ['description'];
			if (empty ( $data ['AdminRole'] ['description'] )) {
				$this->flash ( '角色描述不能为空!', '/Role/add/', 1250 );
				return false;
			}
			$data ['AdminRole'] ['disabled'] = $this->data ['disabled'];
			$data ['AdminRole'] ['listorder'] = $this->data ['listorder'];
			
			$this->loadModel ( "AdminRole" );
			if ($this->AdminRole->add ( $data )) {
				$this->flash ( '添加角色成功!', '/Role/', 1250 );
				return false;
			} else {
				$this->flash ( '添加角色失败!', '/Role/add/', 1250 );
				return false;
			}
		} else {
		}
	}
	
	/**
	 * 修改
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			
			$data ['AdminRole'] ['roleid'] = $this->data ['roleid'];
			if (empty ( $data ['AdminRole'] ['roleid'] )) {
				$this->flash ( '参数错误!', '/Role/', 1250 );
				return false;
			}
			
			$data ['AdminRole'] ['rolename'] = $this->data ['rolename'];
			if (empty ( $data ['AdminRole'] ['rolename'] )) {
				$this->flash ( '角色名称不能为空!', "/Role/edit/{$this->data ['roleid']}/", 1250 );
				return false;
			}
			$data ['AdminRole'] ['description'] = $this->data ['description'];
			if (empty ( $data ['AdminRole'] ['description'] )) {
				$this->flash ( '角色描述不能为空!', "/Role/edit/{$this->data ['roleid']}/", 1250 );
				return false;
			}
			$data ['AdminRole'] ['disabled'] = $this->data ['disabled'];
			$data ['AdminRole'] ['listorder'] = $this->data ['listorder'];
			
			$this->loadModel ( "AdminRole" );
			if ($this->AdminRole->edit ( $data )) {
				$this->flash ( '编辑角色成功!', '/Role/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑角色失败!', "/Role/edit/{$this->data ['roleid']}/", 1250 );
				return false;
			}
		} else {
			// 加载js文件
			$this->set ( 'scriptList', array (
					'role.js' 
			) );
			$roleid = $this->params ['roleid'];
			
			$this->loadModel ( "AdminRole" );
			$RoleInfo = $this->AdminRole->GetAdminRoleByID ( $roleid );
			
			$this->set ( 'RoleInfo', $RoleInfo );
		}
	}
	
	/**
	 * 删除
	 */
	public function delete() {
		$roleid = $this->data ['roleid'];
		
		if (empty ( $roleid )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数不能为空,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "AdminRole" );
		if ($this->AdminRole->del ( $roleid )) {
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除角色成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除角色失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	
	/**
	 * 排序
	 */
	public function listorder() {
		if ($this->request->isPost ()) {
			$listorders = $this->data ['listorders'];
			if (is_array ( $listorders )) {
				$this->loadModel ( "AdminRole" );
				foreach ( $listorders as $roleid => $listorder ) {
					$data ['AdminRole'] ['roleid'] = $roleid;
					$data ['AdminRole'] ['listorder'] = $listorder;
					$this->AdminRole->edit ( $data );
				}
				$this->flash ( '排序操作成功!', "/Role/", 1250 );
				return false;
			}
		} else {
			$this->flash ( '请正确提交角色排序列表!', "/Role/", 1250 );
			return false;
		}
	}
	
	/**
	 * 设置状态
	 */
	public function setStatus() {
		$disabled = $this->data ['disabled'];
		$roleid = $this->data ['roleid'];
		
		if (! isset ( $disabled ) || ! is_numeric ( $disabled ) || empty ( $roleid ) || ! is_numeric ( $roleid )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "AdminRole" );
		$data ['AdminRole'] ['roleid'] = $roleid;
		$data ['AdminRole'] ['disabled'] = $disabled;
		$return = $this->AdminRole->editRole ( $data );
		
		if ($return) {
			$msg = array (
					'rtn_code' => 0,
					'content' => '设置成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,设置失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>